<template>
  <!--推送配置-->
  <div class="search">
    <f-form ref="form" form-key="oss">
      <f-form-item type="other" style="width:100%">
        <template v-for="(item,index) in options">
          <a-select style="width: 80px;" name="serviceName" v-model="item.serviceName">
            <a-select-option :value="item.value" v-for="item in api.common.pushType">{{item.label}}</a-select-option>
          </a-select>
          <a-input placeholder="名称" style="width: 10%;margin-left: 5px" v-model="item.keyName"></a-input>
          <a-input placeholder="appKey" style="width: 30%;margin-left: 5px" v-model="item.appKey"></a-input>
          <a-input placeholder="secretKey" style="width: 35%;margin-left: 5px" v-model="item.secretKey"></a-input>
          <a-switch defaultChecked checkedChildren="正式" unCheckedChildren="开发" v-model="item.ambient" style="margin-left: 5px"/>
          <a-icon class="dynamic-delete-button" type="minus-circle-o" @click="() => remove(index)" v-if="index>0"/>
          <br>
        </template>
      </f-form-item>
      <f-form-item type="other">
        <a-button type="dashed" style="width: 50%" @click="add">
          <a-icon type="plus"/>
          添加推送配置
        </a-button>
      </f-form-item>
    </f-form>
  </div>
</template>

<script>
  export default {
    name: 'pushForm',
    props: {},
    data() {
      return {
        isAdd: true,
        serviceName: 'PUSH_JG',
        options: [{ keyName: '', appKey: '', secretKey: '', ambient: true }],
        config: {
          appKey: '',
          secretKey: ''
        }
      }
    },
    methods: {
      add() {
        this.options.push({ keyName: '', appKey: '', secretKey: '', ambient: true })
      },
      remove(index) {
        this.options.splice(index, 1)
      },
      getData() {
        this.http.get(this, {
          url: this.api.config.get,
          data: {
            configType: 'push'
          },
          success: function(me, data) {
            if (data.PUSH_CONFIG) {
              let result = JSON.parse(data.PUSH_CONFIG)
              me.options = result
            }
          }
        })
      },
      setData() {
        this.$refs.form.resetFields()
        this.getData()
      },
      handleSubmit() {
        this.$refs.form.getForm().validateFields((valid, values) => {
            if (!valid) {
              this.http.post(this, {
                url: this.api.config.pushSave,
                data: {
                  pushConfig: JSON.stringify(this.options)
                },
                success: function(me) {
                }
              })
            }
          }
        )
      }
    },
    computed: {},
    created() {
      //this.getData()
    },
    components: {}
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
